package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.CacheDoubleStaticUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MoreMovieActivity;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.MovieAdapter;
import flc.ast.databinding.FragmentMovieBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResourceBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MovieFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MovieFragment extends BaseNoModelFragment<FragmentMovieBinding> {


    private MovieAdapter movieAdapter;
    private HotAdapter hotAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        movieAdapter=new MovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);

        mDataBinding.rvHotList.setLayoutManager(new GridLayoutManager(mContext,3));
        hotAdapter=new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);
        mDataBinding.ivMovieMore.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivMovieMore:
                startActivity(MoreMovieActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        //清除缓存
        CacheDoubleStaticUtils.clear();
        getMovieData();
        getHotData();
    }

    private void getMovieData(){
        StkResApi.getTagResourceList(null, AppConfig.MOVIE_DATA, StkApi.createParamMap(0, 4),
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                movieAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void getHotData(){
        StkResApi.getTagResourceList(null, AppConfig.HOT_DATA, StkApi.createParamMap(0, 9),
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                hotAdapter.setList(result);
                            }
                        }
                    }
                });
    }

}